<template>
  <div class="departments-container">
    <div class="app-container">
      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <!-- 父向子传子  公司信息 -->
        <treeTools :node-date="company" :is-root="true" @add-depts="addDepts" />

        <!-- 树形控件 -->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <template v-slot="{data}">
            <!-- 父向子传子 -->
            <treeTools
              :node-date="data"
              @del-depts="getDepartments"
              @add-depts="addDepts"
              @edit-depts="editDepts"
            />
          </template>

        </el-tree>
      </el-card>
    </div>

    <!--新增部分的弹出框组件  -->
    <AddDept ref="dept" :list="list" :show-dialog.sync="showDialog" :node-date="nodeDate" @add-dept="getDepartments" />
  </div>
</template>

<script>

import AddDept from '@/views/departments/components/add-dept.vue'

import treeTools from './components/tree-tools'

import { reqGetDepartments } from '@/api/departments'

import { transListToTree } from '@/utils/index'

export default {
  name: 'Departments',
  components: {
    treeTools,
    AddDept
  },
  data() {
    return {
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: {},
      showDialog: false,
      nodeDate: {},
      list: [],
      // 进度条
      loading: true
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async  getDepartments() {
      this.loading = true
      const { data: { depts, companyName }} = await reqGetDepartments()

      this.list = depts

      // 部门的书型数据
      this.departs = transListToTree(depts, '')
      // 公司信息
      this.company = { name: companyName, manager: '负责人', id: '' }
      this.loading = false
    },
    addDepts(data) {
      // 显示弹出框
      this.showDialog = true
      // 把父部门的数据传到弹出层
      this.nodeDate = data
    },
    // 编辑
    editDepts(data) {
      this.showDialog = true
      this.nodeDate = data

      // 获取部门的详情,父组件通过refs可以调用子组件内部的方法
      this.$nextTick(() => {
        this.$refs.dept.getDepartDetail()
      })
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
.el-tree {
  ::v-deep {
    // 小三角的样式, 去掉默认的小三角的旋转效果
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    // 有子节点 且未展开 +
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 有子节点 且已展开 -
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    // 没有子节点
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
